﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

using Game.UI;

namespace Game
{
    /// <summary>
    /// 实现简单的切换动画帧、UI帧
    /// </summary>
	public class SimpleAnimation : MonoBehaviour 
	{
        
        [SerializeField]float m_aniFrameDeltaTime = 0.03f;
        [SerializeField]Sprite [] m_imgsToSwitch;

        /// <summary>
        /// 用于判断当前添加的动画类型
        /// </summary>
        enum AniObjType { None, UI, Sprite };
        AniObjType m_aniObjType;

        SpriteRenderer m_spRenderer;
        Image m_uiRenderer;

        #region Public area

        /// <summary>
        /// 初始化某个物体，为之增加动画帧与更新时间
        /// </summary>
        /// <param name="imgFrams">用初始化的Texture动画帧</param>
        /// <param name="updateTime">播放帧动画的间隔</param>
        public static void InitAnimationTo(GameObject objTarget, Texture2D[] textureFrames,float updateTime=-1f)
        {
            //从Texture2D中创建出Sprites
            int frameCount = textureFrames.Length;
            Sprite[] sprites = new Sprite[frameCount];
            for (int i = 0; i < frameCount; i++)
            {
                sprites[i] = textureFrames[i].ToSprite();
            }

            //--真正的初始化
            InitAnimationTo(objTarget, sprites, updateTime);
        }

        /// <summary>初始化某个物体，为之增加动画帧与更新时间初始化帧与更新时间
        /// </summary>
        /// <param name="imgFrams">用初始化的Sprite动画帧</param>
        /// <param name="updateTime">播放帧动画的间隔</param>
        public static void InitAnimationTo(GameObject objTarget, Sprite [] spriteFrames, float updateTime = -1f)
        {
            //--看这个物体之前是否有附加过Simple动画组件,无则附加之
            SimpleAnimation simpleAni = objTarget.GetComponent<SimpleAnimation>();
            if (simpleAni == null)
            {
                simpleAni = objTarget.AddComponent<SimpleAnimation>();
            }

            //--只有有效值才设置，否则采用默认值0.3f
            if (updateTime > 0)
                simpleAni.m_aniFrameDeltaTime = updateTime;

            //--设置用于动画更新的帧s
            simpleAni.m_imgsToSwitch = spriteFrames;

            //--重新Start这个animation:
            simpleAni.DoStart();
        }
        #endregion

        #region Private area:
        //用于区分是Init过了没，避免第一次Start时重复Init
        bool m_inited = false;

        // Use this for initialization
		void Start () 
		{
            if(m_inited==false)
                DoStart();
		}

        private void DoStart()
        {
            m_inited = true;

            if (m_imgsToSwitch.Length <= 0)
            {
                Debug.LogError("No imgs specified.");
                return;
            }

            m_spRenderer = this.GetComponent<SpriteRenderer>();
            m_uiRenderer = this.GetComponent<Image>();

            if (m_spRenderer != null)
            {
                m_aniObjType = AniObjType.Sprite;
            }
            else if (m_uiRenderer != null)
            {
                m_aniObjType = AniObjType.UI;
            }
            else
            {
                m_aniObjType = AniObjType.None;
            }

            //Has ani:
            if (m_aniObjType != AniObjType.None)
            {
                //--gif只有一帧的情况下，则不用开启协程切换：
                if (m_imgsToSwitch.Length == 1)
                {
                    SetFrame(m_imgsToSwitch[0]);
                }
                else
                {
                    //启动ani更新：
                    StartCoroutine(
                        GameObjFunc.IUpdateDo(DoUpdateAni, m_aniFrameDeltaTime));
                }
            }
        }
		
		// DoUpdate is called once per deltaTime
        int m_currImgFrame = 0;
		void DoUpdateAni () 
		{
            m_currImgFrame = (m_currImgFrame + 1) % m_imgsToSwitch.Length;

            SetFrame(m_imgsToSwitch[m_currImgFrame]);
        }

        private void SetFrame(Sprite newSprite)
        {
            //间隔更新，则不断切换图片
            switch (m_aniObjType)
            {
                case AniObjType.Sprite:
                    m_spRenderer.sprite = newSprite;
                    break;

                case AniObjType.UI:

                    m_uiRenderer.sprite = newSprite;

                    break;

                case AniObjType.None:
                    Debug.LogError("Illegal param:");
                    break;
            }
        }

        #endregion
    }

}